package com.tn.util;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.tn.bo.CreditMemo;
import com.tn.bo.Invoice;
import com.tn.bo.POHeader;
import com.tn.bo.Payment;
import com.tn.bo.UserProfile;

public class MailService {
	public void sendPOReceMail(String mailaddress,POHeader poHeader) throws Exception{
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Purchase Order Received "+poHeader.toString();    
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
			msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
			msg.setSubject("New Purchase Order Received: "+poHeader.getOrderNumber());           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
			System.out.println(e);
				
		}
	}
	
	public void sendRegMail(String mailaddress,String  regHash) throws Exception{
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);     
		//String msgBody = "A New Purchase Order From TradeNetwork4u "; 
		String sender = "admin@atsocii.com"; 
		   
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress(sender, "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
			//msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
			msg.setSubject("Registration confirmation for Trade Network");  
			
			String s=
			"Dear User,\n"+
			"Thank you for registering on www.atsocii.com.\n"+
			"To complete your registration, please click on the below link. Once the registration is completed, you will be asked to associate yourself with a company.\n " +
			"If you are the first user from your company, you will be asked to register the company.\n"+
			"Otherwise, you can search for your company and the super user in your company will be notified to approve the association.\n"+
			"Link:\n ";
			
			String dyamicLink = "http://www.atsocii.com/register?"+regHash+"\n";
			System.out.println("DYNALINK:  http://www.atsocii.com/register?" + regHash);
			String s2= "Best Regards,\n"+
			"Management of AtSocii.com";
			
			String msgBody1 = s+dyamicLink+s2;
			
			
			//String dyamicLink = "test";
			//Html email causing some problem????(:
			/*String htmlBody = "<html><body><a href="+dyamicLink+">Confirm My Trade Nework Regisration</a></body></html>";     
	        //System.out.println(htmlBody);
	        Multipart mp = new MimeMultipart();
	        MimeBodyPart htmlPart = new MimeBodyPart();
	        htmlPart.setContent(htmlBody, "text/html");
	        mp.addBodyPart(htmlPart);*/
	        msg.setText(msgBody1);            
			Transport.send(msg);   
			
			} catch (Exception e) {
			throw e;
		}
	}
	
	public void sendForgotPasswordMail(String username,String pwdHash) throws Exception{
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
	
			String dyamicLink = "http://www.atsocii.com/forgot?"+pwdHash+"\n";
			String msgBody=
				"Dear User,\n"+
				"Thank you for registering on www.atsocii.com.\n"+
				"To reset your password, please click on the below link.\n"+
				"Link:\n "+dyamicLink +"\n";
				
			//System.out.println("DYNALINK:  http://www.atsocii.com/forgot?" + pwdHash);
			msgBody += "Best Regards,\n"+
			"Management of AtSocii.com";
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(username, "Dear User"));
			msg.setSubject("Reset Password");           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
				throw e;
				
		}
	}
	
	
	public void sendApprovalMail(String username,UserProfile newuserProfile) throws Exception{
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody=
				"Dear User,\n"+
				"Follwoing user wants approval.\n"+
				newuserProfile.getFirstName()+newuserProfile.getLastName();
				
				
			//System.out.println("DYNALINK:  http://www.atsocii.com/forgot?" + pwdHash);
			msgBody += "Best Regards,\n"+
			"Management of AtSocii.com";
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(username, "Dear User"));
			msg.setSubject("New Appproval Request");           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
				throw e;
				
		}
	}
	
	public void sendApprovedMail(UserProfile newuserProfile) throws Exception{
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody=
				"Dear,"+newuserProfile.getFirstName()+newuserProfile.getLastName()+"\n"+
				"Your Request is Approved From Your Company Admin";
				
			//System.out.println("DYNALINK:  http://www.atsocii.com/forgot?" + pwdHash);
			msgBody += "Best Regards,\n"+
			"Management of AtSocii.com";
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(newuserProfile.getEmailAddress(), "Dear User"));
			msg.setSubject("New Appproval Request");           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
				throw e;
				
		}
	}

	public void sendPOSenderMail(String mailaddress, POHeader poHeader) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Purchase Sent From Your Company "+poHeader.toString();    
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
			msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
			msg.setSubject("New Purchase Order Generated From Your Company : "+poHeader.getOrderNumber());           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
			System.out.println(e);
				
		}
		
	}

	public void sendInvSentMail(String mailaddress, Invoice invoice) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Invoice Order Sent From Your Company "+invoice.getInvoiceNumber();    
		try {           
			Message msg = new MimeMessage(session);            
			msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
			msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
			msg.setSubject("New Invoice Sent  : "+invoice.getInvoiceNumber());           
			msg.setText(msgBody);            
			Transport.send(msg);        
			} catch (Exception e) {
			System.out.println(e);
				
		}
		
	}

	public void sendInvRecvMail(String mailaddress, Invoice invoice) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Invoice Order Received To Your Company "+invoice.getInvoiceNumber();    
	try {           
		Message msg = new MimeMessage(session);            
		msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
		msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
		msg.setSubject("New Invoice Received : "+invoice.getInvoiceNumber());           
		msg.setText(msgBody);            
		Transport.send(msg);        
		} catch (Exception e) {
				System.out.println(e);
			}
	}

	public void sendCreditSentMail(String mailaddress, CreditMemo cmemo) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Credit Memo Sent From Your Company "+cmemo.getCreditMemoNumber();    
	try {           
		Message msg = new MimeMessage(session);            
		msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
		msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
		msg.setSubject("New Credit Memo Send : "+cmemo.getCreditMemoNumber());           
		msg.setText(msgBody);            
		Transport.send(msg);        
		} catch (Exception e) {
				System.out.println(e);
			}
	}

	public void sendCreditRecvMail(String mailaddress, CreditMemo cmemo) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Credit Memo Received To Your Company "+cmemo.getCreditMemoNumber();    
	try {           
		Message msg = new MimeMessage(session);            
		msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailaddress, "Dear User"));
		msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
		msg.setSubject("New Credit Memo Received : "+cmemo.getCreditMemoNumber());           
		msg.setText(msgBody);            
		Transport.send(msg);        
		} catch (Exception e) {
				System.out.println(e);
			}
	}

	public void sendPaymentSentMail(String email, Payment payment) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Payment Sent From Your Company "+payment.getPaymentNumber();    
	try {           
		Message msg = new MimeMessage(session);            
		msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, "Dear User"));
		msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
		msg.setSubject("A New Payment Sent From Your Company : "+payment.getPaymentNumber());           
		msg.setText(msgBody);            
		Transport.send(msg);        
		} catch (Exception e) {
				System.out.println(e);
			}
	}

	public void sendPaymentRecvMail(String email, Payment payment) {
		Properties props = new Properties();        
		Session session = Session.getDefaultInstance(props, null);        
		String msgBody = "A New Payment Received To Your Company "+payment.getPaymentNumber();    
	try {           
		Message msg = new MimeMessage(session);            
		msg.setFrom(new InternetAddress("admin@atsocii.com", "www.atsocii.com")); 
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, "Dear User"));
		msg.addRecipient(Message.RecipientType.CC, new InternetAddress("rathi.aditya@gmail.com", "Dear User"));   
		msg.setSubject("A New Payment Received To Your Company : "+payment.getPaymentNumber());           
		msg.setText(msgBody);            
		Transport.send(msg);        
		} catch (Exception e) {
				System.out.println(e);
			}
	}
}
